
/*
 * ManejadorAbstracto.java
 *
 * 
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Servicio;
import java.sql.*;
import java.util.*;

/**
 * Clase Template para crear objetos y guerdarlos 
 * @author seba y gi
 */
public abstract class ManejadorAbstracto {
    private Statement statement;
    protected ResultSet resultSet;
    protected int opcion=0;
    protected String valor="0";
    protected String valor2="0";
    protected int indice=0;
    
    /**
     * Creates a new instance of ManejadorAbstracto
     */
    public ManejadorAbstracto() {
       
    }
    /** Consulta para traer los elementos requeridos */
    protected abstract String consulta(); 
    
    /** Tiene el nuevo objeto */
    protected abstract Object nuevo(); 
    
    /** Configura distintas consultas */
    public void setFiltro(int opc, String val, String val2){
        this.valor=val;
        this.valor2=val2;
        this.opcion=opc;
    }
    
    public void setIndice(int i){
        this.indice=i;
    }
    
    /** Instanciador de objetos */
    public ArrayList<Object> FactoryMethod() {
       ArrayList<Object> aux = new ArrayList<Object>();
       Conexion con = Conexion.getInstance();
       con.setConection();
       try {
       statement = con.getConexion().createStatement();
       resultSet = statement.executeQuery(consulta());
       while (resultSet.next()){       
            aux.add(nuevo()); 
       }
       } catch (Exception e) {e.printStackTrace();  }
       try {
            statement.close(); 
       } catch (Exception e) {e.printStackTrace();  }
       con.closeConection();
       return aux;
    }
    
        protected abstract String table(); 
        
        protected abstract String values(Object objeto); 
       
       /** Estatizador de objetos */
        public boolean insert(Object objeto){
        Conexion con = Conexion.getInstance();
       con.setConection();
       try {
       
       Statement statement = con.getConexion().createStatement();    
       String aux = "insert into "+table()+" values"+values(objeto);
       System.out.println(aux);
       statement.execute(aux);
       statement.close();
       
   
        } catch (Exception e) {
            e.printStackTrace();
            con.closeConection();
            return false;
        }
        con.closeConection();
        return true;
        }
        /** Actualizador de valores*/ 
      
        public boolean update(Object objeto){
        Conexion con = Conexion.getInstance();
        con.setConection();
            try {
       
       Statement statement = con.getConexion().createStatement();    
       String aux = "Update "+table_u()+" SET "+campos_u(objeto)+" Where "+ides_u(objeto);
       statement.execute(aux);
       statement.close();
   
        } catch (Exception e) {
            e.printStackTrace();
            con.closeConection();
            return false;
        }
        con.closeConection();
        return true;
      }
        
        public void delete(Object objeto){
       Conexion con = Conexion.getInstance();
       con.setConection();
            try {
       
       Statement statement = con.getConexion().createStatement();    
       String aux = "Delete From "+table_d()+" Where "+campos_d(objeto);
       statement.execute(aux);
       statement.close();
   
        } catch (Exception e) {
            e.printStackTrace();
        }
       con.closeConection();
      }
      
        protected abstract String table_u();
        
        protected abstract String campos_u(Object objeto);
 
        protected abstract String ides_u(Object objeto);
        
        protected abstract String table_d();
        
        protected abstract String campos_d(Object objeto);
}
    
    
            

 
    
